/// <reference path="Addition.js" />
Type.registerNamespace("BM");

// Constraint object

BM.Constraint = function(item, data) {
    ///	<summary>
    ///	Constraint is a special type of Addition, it is pinned in items' validation process.
    /// In order for the item to be valid, all its constraints must be met.
    ///	</summary>
    ///	<returns type="BM.Constraint" />
    BM.Constraint.initializeBase(this, [item, data]);
}

BM.Constraint.prototype = {
    load: function () {
        BM.Constraint.callBaseMethod(this, 'load');
    },

    getErrorText: function () {
        /// <returns type="String" mayBeNull="true" >
        /// gets the attached error text
        /// </returns>
        if (this.errorText) {
            return this.errorText;
        }
        if (this.getProperty('errorText')) {
            this.errorText = this.getProperty('errorText');
            return this.errorText;
        }
        return this.getDefaultErrorText();
    },

    getDefaultErrorText: function () {
        /// <returns type="String" mayBeNull="true" >
        /// gets the error text that should be displayed when there is no attached error text
        /// </returns>
        return null;
    },

    setErrorText: function (value) {
        /// <summary>
        /// sets the text to be shown on error
        /// </summary>
        /// <param name="value" type="String" mayBeNull="true">
        /// Event handler
        /// </param>
        this.errorText = value;
    },

    addMet: function (handler) {
        /// <summary>
        /// Add an event handler for the Met event
        /// </summary>
        /// <param name="handler" type="Function" mayBeNull="false">
        /// Event handler
        /// </param>
        /// <returns />
        this.get_events().addHandler('met', handler);
    },

    removeMet: function (handler) {
        /// <summary>
        /// Remove an event handler from the Met event
        /// </summary>
        /// <param name="handler" type="Function" mayBeNull="false">
        /// Event handler
        /// </param>
        /// <returns />
        this.get_events().removeHandler('met', handler);
    },

    raiseMet: function (eventArgs) {
        /// <summary>
        /// Raise the Met event
        /// </summary>
        /// <param name="eventArgs" type="Sys.EventArgs" mayBeNull="false">
        /// Event arguments for the Met event
        /// </param>
        /// <returns />

        var handler = this.get_events().getHandler('met');
        if (handler) {
            handler(this, eventArgs);
        }
    },

    addNotMet: function (handler) {
        /// <summary>
        /// Add an event handler for the NotMet event
        /// </summary>
        /// <param name="handler" type="Function" mayBeNull="false">
        /// Event handler
        /// </param>
        /// <returns />
        this.get_events().addHandler('notMet', handler);
    },

    removeNotMet: function (handler) {
        /// <summary>
        /// Remove an event handler from the NotMet event
        /// </summary>
        /// <param name="handler" type="Function" mayBeNull="false">
        /// Event handler
        /// </param>
        /// <returns />
        this.get_events().removeHandler('notMet', handler);
    },

    raiseNotMet: function (eventArgs) {
        /// <summary>
        /// Raise the NotMet event
        /// </summary>
        /// <param name="eventArgs" type="Sys.EventArgs" mayBeNull="false">
        /// Event arguments for the NotMet event
        /// </param>
        /// <returns />

        var handler = this.get_events().getHandler('notMet');
        if (handler) {
            handler(this, eventArgs);
        }
    },

    refresh: function () {
        ///	<summary>
        ///		refreshes the constraint by running validateSelf on parent item
        ///	</summary>
        this.getItem().validateSelf();
    },

    check: function () {
        ///	<summary>
        ///		returns information if the validation on the constraint was successful        
        ///		and performs attached events
        ///	</summary>
        ///	<returns type="Boolean" />
        var isMet = this.isMet();
        if (isMet) {
            this.raiseMet()
        } else {
            this.raiseNotMet();
        }
        return isMet;
    },

    isMet: function () {
        ///	<summary>
        ///		returns information if the validation on the constraint was successful                
        ///	</summary>
        ///	<returns type="Boolean" />        
        return true;
    }
}

BM.Constraint.registerClass("BM.Constraint", BM.Addition);